<?php

class SRate_5h
{

    public $disk = "a";
    public $lotto_id = 0;
    public $table_name = "";
    public $tableS_name = "";
    public $db;
    public $diskStrArr = array ( "a" , "b" , "c" , "d" );
    public $diskStrArrB = array ( "A 盘" , "B 盘" , "C 盘" , "D 盘" );
    public $tbl;
    public $gametype;
    public $betXYArr = array ( );
    public $result_type = MYSQL_BOTH;
    public $tR21Str = "";
    public $r21Flag = false;
    public $maxSubid = 5;

    public function SRate_5h($gametype = 20)
    {
        global $g_gametype_3d;
        global $g_betXYArr;
        if ( ! in_array($gametype , array ( 20 , 21 , 22 , 23 , 24 )) )
        {
            exit();
        }
        $this->db = new DB();
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->gametype = $gametype;
        $this->betXYArr = $g_betXYArr[$gametype];
        $this->table_name = "rate_{$this->tbl}";
        $this->tableS_name = "rates_{$this->tbl}";
        if ( $gametype == 20 )
        {
            $this->tR21Str = ",r21";
            $this->r21Flag = true;
            $this->maxSubid = 5;
        }
        else
        {
            $this->tR21Str = "";
            $this->r21Flag = false;
            $this->maxSubid = 8;
        }
    }

    public function getRates($lotto_id , $xyid , $disk , $bettype , $subid)
    {
        if ( in_array($bettype , $this->betXYArr) )
        {
            if ( $xyid < 1 )
            {
                $xyidStr = "AND xyid = 1";
            }
            else
            {
                $xyidStr = "AND xyid = {$xyid}";
            }
        }
        else
        {
            $xyidStr = "AND xyid = 0";
        }
        $sql = "!!SELECT r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20{$this->tR21Str} FROM {$this->table_name} WHERE lotto_id = {$lotto_id} AND bettype = {$bettype} {$xyidStr} AND disk = {$disk} AND  subid = {$subid} LIMIT 1";
		$result = $this->db->query($sql);
        if ( $row = mysql_fetch_array($result , $this->result_type) )
        {
            return $row;
        }
        return false;
    }

    public function getRatesS($lotto_id , $bettype , $user_id , $subid = 0)
    {
        $sql = "!!SELECT r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20{$this->tR21Str} FROM {$this->tableS_name} WHERE lotto_id = {$lotto_id} AND user_id = {$user_id} AND bettype = {$bettype} AND  subid = {$subid} LIMIT 1";
		$result = $this->db->query($sql);
        if ( $row = mysql_fetch_array($result , $this->result_type) )
        {
            return $row;
        }
        return false;
    }

    public function addRates($lotto_id , $xyid , $disk , $bettype , $subid , $rateArr)
    {
        $feStr = "";
        $valStr = "";
        foreach ( $rateArr as $key => $val )
        {
            $feStr .= ",".$key;
            $valStr .= ",".$val;
        }
        $sql = "INSERT INTO {$this->table_name} (lotto_id, xyid, disk, bettype, subid {$feStr})";
        $sql .= " VALUES({$lotto_id}, {$xyid}, {$disk}, {$bettype}, {$subid} {$valStr})";
        return $this->db->query($sql);
    }

    public function updateRate($lotto_id , $xyid , $disk , $bettype , $subid , $rateArr)
    {
        $sqlArr = array ( );
        if ( $lotto_id )
        {
            foreach ( $rateArr as $key => $val )
            {
                $sqlArr[] = " A.{$key} = IF({$val} > B.{$key} , B.{$key}, {$val}) ";
            }
            $sqlStr = implode("," , $sqlArr);
            $sql = "UPDATE {$this->table_name} A, {$this->table_name} B SET {$sqlStr} ";
            $sql .= " WHERE A.lotto_id = {$lotto_id} AND A.disk = {$disk} AND A.bettype = {$bettype} AND A.subid = {$subid} AND A.xyid = {$xyid}";
            $sql .= " AND B.lotto_id = 0 AND B.disk = {$disk} AND B.bettype = {$bettype} AND B.subid = {$subid} AND B.xyid = {$xyid}";
        }
        else
        {
            foreach ( $rateArr as $key => $val )
            {
                $sqlArr[] = "{$key} = {$val}";
            }
            $sqlStr = implode("," , $sqlArr);
            $sql = "UPDATE {$this->table_name} SET {$sqlStr} ";
            $sql .= " WHERE lotto_id = {$lotto_id} AND disk = {$disk} AND bettype = {$bettype} AND subid = {$subid} AND xyid = {$xyid}";
        }
        return $this->db->query($sql);
    }

    public function updateRateByAddVal($lotto_id , $bettype , $subid , $rateArr)
    {
        $sqlArr = array ( );
        if ( $lotto_id )
        {
            foreach ( $rateArr as $key => $val )
            {
                $val = floatval($val);
                $sqlArr[] = "A.{$key} = IF((IF((A.{$key} + {$val}) < 0, 0, (A.{$key} + {$val})) > B.{$key}), B.{$key}, (IF((A.{$key} + {$val}) < 0, 0, (A.{$key} + {$val}))))";
            }
            $sqlStr = implode("," , $sqlArr);
            $sql = "UPDATE {$this->table_name} A, {$this->table_name} B SET {$sqlStr} ";
            $sql .= " WHERE A.lotto_id = {$lotto_id} AND A.bettype = {$bettype} AND A.subid = {$subid}";
            $sql .= " AND B.lotto_id = 0 AND A.bettype = B.bettype AND A.xyid = B.xyid AND A.subid = B.subid AND A.disk = B.disk ";
        }
        else
        {
            foreach ( $rateArr as $key => $val )
            {
                $val = floatval($val);
                $sqlArr[] = "{$key} = IF(({$key} + {$val}) < 0, 0, ({$key} + {$val}))";
            }
            $sqlStr = implode("," , $sqlArr);
            $sql = "UPDATE {$this->table_name} SET {$sqlStr} ";
            $sql .= " WHERE lotto_id = {$lotto_id} AND bettype = {$bettype} AND subid = {$subid}";
        }
        return $this->db->query($sql);
    }

    public function updateRateByMltVal($lotto_id , $bettype , $subid , $rateArr)
    {
        $sqlArr = array ( );
        if ( $lotto_id )
        {
            foreach ( $rateArr as $key => $val )
            {
                $val = floatval($val);
                $sqlArr[] = "A.{$key} = IF((IF((A.{$key} * {$val}) < 0, 0, (A.{$key} * {$val})) > B.{$key}), B.{$key}, (IF((A.{$key} * {$val}) < 0, 0, (A.{$key} * {$val}))))";
            }
            $sqlStr = implode("," , $sqlArr);
            $sql = "UPDATE {$this->table_name} A, {$this->table_name} B SET {$sqlStr} ";
            $sql .= " WHERE A.lotto_id = {$lotto_id} AND A.bettype = {$bettype} AND A.subid = {$subid}";
            $sql .= " AND B.lotto_id = 0 AND A.bettype = B.bettype AND A.xyid = B.xyid AND A.subid = B.subid AND A.disk = B.disk ";
        }
        else
        {
            foreach ( $rateArr as $key => $val )
            {
                $val = floatval($val);
                $sqlArr[] = "{$key} = IF(({$key} * {$val}) < 0, 0, ({$key} + {$val}))";
            }
            $sqlStr = implode("," , $sqlArr);
            $sql = "UPDATE {$this->table_name} SET {$sqlStr} ";
            $sql .= " WHERE lotto_id = {$lotto_id} AND bettype = {$bettype} AND subid = {$subid}";
        }
        return $this->db->query($sql);
    }

    public function addUpdRatesS($user_id , $lotto_id , $bettype , $subid , $rateArr)
    {
        $feStr = "";
        $valStr = "";
        $updArr = array ( );
        foreach ( $rateArr as $key => $val )
        {
            $feStr .= ",".$key;
            $valStr .= ",".$val;
            $updArr[] = " {$key} = {$key} + {$val} ";
        }
        $sql = "INSERT INTO {$this->tableS_name} (`lotto_id`,`user_id`,`bettype`,`subid` {$feStr})";
        $sql .= " VALUES({$lotto_id}, {$user_id}, {$bettype}, {$subid} {$valStr})";
        $sql .= " ON DUPLICATE KEY UPDATE ";
        $sql .= implode("," , $updArr);
        return $this->db->query($sql);
    }

    public function updateRateS($user_id , $lotto_id , $bettype , $subid , $rateArr)
    {
        $feStr = "";
        $valStr = "";
        $updArr = array ( );
        foreach ( $rateArr as $key => $val )
        {
            if ( $val < 0 )
            {
                $val *= 0 - 1;
            }
            $feStr .= ",".$key;
            $valStr .= ",".$val;
            $updArr[] = " {$key} = {$val} ";
        }
        $sql = "INSERT INTO {$this->tableS_name} (`lotto_id`,`user_id`,`bettype`,`subid` {$feStr})";
        $sql .= " VALUES({$lotto_id}, {$user_id}, {$bettype}, {$subid} {$valStr})";
        $sql .= " ON DUPLICATE KEY UPDATE ";
        $sql .= implode("," , $updArr);
        return $this->db->query($sql);
    }

    public function getLotto3DByDate($day)
    {
        if ( $this->gametype == 24 )
        {
            $sql = "SELECT * FROM lotto_{$this->tbl} WHERE '".date("Y-m-d H:i:s")."' <= stopbet AND open != '0000-00-00' ORDER BY stopbet limit 0,1";
        }
        else
        {
            $sql = "SELECT * FROM lotto_{$this->tbl} WHERE '".date("Y-m-d H:i:s")."' <= openbet AND open != '0000-00-00' ORDER BY openbet limit 0,1";
        }
        $result = $this->db->query($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

    public function getLotto3DById($id)
    {
        $sql = "SELECT * FROM lotto_{$this->tbl} WHERE id='{$id}' limit 1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

    public function getLotto3DByIdNext($id)
    {
        $sql = "SELECT * FROM lotto_{$this->tbl} WHERE id > '{$id}' AND open != '0000-00-00' ORDER BY id ASC limit 0,1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

    public function getLotto3DByIdPre($id)
    {
        $sql = "SELECT * FROM lotto_{$this->tbl} WHERE id < '{$id}' AND open != '0000-00-00' ORDER BY id DESC limit 0,1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

    public function getAcl5ID($user_id , $acl)
    {
        $sql = "";
        switch ( $acl )
        {
            case 1 :
                $sql = "SELECT a.id FROM k_user a \r\n              LEFT JOIN k_user b ON a.id = b.prev_id \r\n              LEFT JOIN k_user c ON b.id = c.prev_id \r\n              LEFT JOIN k_user d ON c.id = d.prev_id\r\n              LEFT JOIN k_user e ON d.id = e.prev_id\r\n              WHERE e.id={$user_id} AND a.acl = 5";
                break;
            case 2 :
                $sql = "SELECT a.id FROM k_user a \r\n              LEFT JOIN k_user b ON a.id = b.prev_id \r\n              LEFT JOIN k_user c ON b.id = c.prev_id \r\n              LEFT JOIN k_user d ON c.id = d.prev_id\r\n              WHERE d.id={$user_id} AND a.acl = 5";
                break;
            case 3 :
                $sql = "SELECT a.id FROM k_user a \r\n              LEFT JOIN k_user b ON a.id = b.prev_id \r\n              LEFT JOIN k_user c ON b.id = c.prev_id \r\n              WHERE c.id={$user_id} AND a.acl = 5";
                break;
            case 4 :
                $sql = "SELECT a.id FROM k_user a \r\n              LEFT JOIN k_user b ON a.id = b.prev_id \r\n              WHERE b.id={$user_id} AND a.acl = 5";
                break;
            case 5 :
                return $user_id;
            default :
                return false;
        }
        $result = $this->db->query($sql);
        if ( $row = mysql_fetch_array($result , MYSQL_NUM) )
        {
            return $row[0];
        }
        return false;
    }

    public function updateNowRatetoBaseRate($lotto_id)
    {
        $error = 0;
        $sqlA = "!!SELECT r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20{$this->tR21Str}, bettype, subid, xyid, disk FROM {$this->table_name} ";
        $sqlA .= " WHERE lotto_id = 0";
        $result = $this->db->query($sqlA);
        while ( $row = mysql_fetch_array($result) )
        {
            $sql = "UPDATE {$this->table_name} SET r01 = {$row[0]} ,r02 = {$row[1]} ,r03 = {$row[2]} ,r04 = {$row[3]} ,r05 = {$row[4]} ,r06 = {$row[5]} ,r07 = {$row[6]} ,r08 = {$row[7]} ,r09 = {$row[8]} ,r10 = {$row[9]} ,r11 = {$row[10]} ,r12 = {$row[11]} ,r13 = {$row[12]} ,r14 = {$row[13]} ,r15 = {$row[14]} ,r16 = {$row[15]} ,r17 = {$row[16]} ,r18 = {$row[17]} ,r19 = {$row[18]} ,r20 = {$row[19]}";
            if ( $this->r21Flag )
            {
                $sql .= " ,r21 = {$row[20]} ";
            }
            $sql .= " WHERE lotto_id = {$lotto_id} AND disk =  ".$row['disk']." AND bettype = ".$row['bettype']." AND subid = ".$row['subid']." AND xyid = ".$row['xyid'];
            if ( $this->db->query($sql) == false )
            {
 ++ $error;
            }
        }
        return $error;
    }

    public function copyRateBet($lotto_id)
    {
        $error = 0;
        $xyid = 0;
        if ( $this->r21Flag )
        {
            $subidStr = "2,3,4,5";
        }
        else
        {
            $subidStr = "2,3,4,5,6,7,8";
        }
        $disk = 1;
        while ( $disk < 5 )
        {
            $sqlA = "!!SELECT r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20{$this->tR21Str} FROM {$this->table_name} ";
            $sqlA .= " WHERE lotto_id = {$lotto_id} AND disk = {$disk} AND bettype = 0 AND subid = 1 AND xyid = {$xyid}";
            $result = $this->db->query($sqlA);
            if ( $row = mysql_fetch_row($result) )
            {
                $sql = "UPDATE {$this->table_name} SET r01 = {$row[0]} ,r02 = {$row[1]} ,r03 = {$row[2]} ,r04 = {$row[3]} ,r05 = {$row[4]} ,r06 = {$row[5]} ,r07 = {$row[6]} ,r08 = {$row[7]} ,r09 = {$row[8]} ,r10 = {$row[9]} ,r11 = {$row[10]} ,r12 = {$row[11]} ,r13 = {$row[12]} ,r14 = {$row[13]} ,r15 = {$row[14]} ,r16 = {$row[15]} ,r17 = {$row[16]} ,r18 = {$row[17]} ,r19 = {$row[18]} ,r20 = {$row[19]}  ";
                if ( $this->r21Flag )
                {
                    $sql .= ",r21 = {$row[20]} ";
                }
                $sql .= " WHERE lotto_id = {$lotto_id} AND disk = {$disk} AND bettype = 0 AND subid IN ({$subidStr}) AND xyid = {$xyid}";
                $this->db->query($sql);
            }
            else
            {
 ++ $error;
            }
            ++ $disk;
        }
        return $error;
    }

    public function updateRateSynGametype()
    {
        global $g_gametype_3d;
        $gamtypeArr = $g_gametype_3d;
        $fStr = "`lotto_id`, `bettype`, `xyid`, `subid`, `disk`, `r01`, `r02`, `r03`, `r04`, `r05`, `r06`, `r07`, `r08`, `r09`, `r10`, `r11`, `r12`, `r13`, `r14`, `r15`, `r16`, `r17`, `r18`, `r19`, `r20`";
        if ( substr($g_gametype_3d[$this->gametype]['en'] , 0 , 2) == "8h" )
        {
            $rateTblB = "rate_{$this->tbl}";
            $okI = 0;
            for ( $i = 21; $i < 24; $i ++  )
            {
                $rateTblA = "rate_".$gamtypeArr[$i]['en'];
                if ( $i == $this->gametype )
                {
                    continue;
                }
                $sql = "INSERT INTO {$rateTblA} ( {$fStr} )";
                $sql .= " SELECT {$fStr} FROM {$rateTblB} AS b";
                $sql .= " WHERE b.lotto_id = 0 ";
                $sql .= " ON DUPLICATE KEY UPDATE ";
                $sql .= "  r01 = b.r01, r02 = b.r02, r03 = b.r03, r04 = b.r04, r05 = b.r05";
                $sql .= ", r06 = b.r06, r07 = b.r07, r08 = b.r08, r09 = b.r09, r10 = b.r10";
                $sql .= ", r11 = b.r11, r12 = b.r12, r13 = b.r13, r14 = b.r14, r15 = b.r15";
                $sql .= ", r16 = b.r16, r17 = b.r17, r18 = b.r18, r19 = b.r19, r20 = b.r20";
                if ( $this->db->query($sql) )
                {
 ++ $okI;
                }
            }
            return $okI;
        }
        return false;
    }

}

?>